import java.net.*;
import java.io.*;

/*
 * UDP server listens for a request 
 * and sends it back to the client
 */
public class UDPServer{
    private int serverPort = 2222;
	private byte[] buffer = new byte[1000];
	//list of clients currently monitored
	private List<String> clientList = new ArrayList<String>(); 

	public static void main(String args[]){
        DatagramSocket aSocket = null;
        try{
			aSocket = new DatagramSocket(serverPort); //bound to host and port
			while(true){
				DatagramPacket request = new DatagramPacket(buffer, buffer.length);
				aSocket.receive(request);//blocked if no input
				
				//Call appropriate methods upon receiving a request
				
				
				//To reply, send back to the same port(in this example)
				DatagramPacket reply = new DatagramPacket(
					request.getData(),request.getLength(),request.getAddress(),request.getPort());
					aSocket.send(reply);
			}
		}
		catch(Exception e) {
			//handle exceptions
		} finally {
			if (aSocket!=null) {
				aSocket.close();
			}
		}
	}
	
   /**
	* Register client to be monitored to server
	*/
	private void registerClient(String filename,String interval,String address,String portNum) {
		//clientList.add...
	}
	
   /**
	* Check for clients registered to server to be monitored
	* and update the respective clients
	*/
	private void updateMonitoredClients(String filename) {
		if(!clientList.isEmpty) {
			/*Send reply to client that wishes to 
			be updated for the specified file*/
			
		}
	}
	
	private void writeFile(String filename) {
		//1. update the file
		//2. reply to client
		//3. update monitored clients
		updateMonitoredClients(filename);
	}
	
	private void readFile(String filename) {
		
	}
}